#include "Lexer.h"
#include "../VirtualMachine.h"
#include <iostream>
#include <string>
#include <cmath>
#include <map>

/**
* 字节码分析器
*/

struct SET_VAL{
    string *name;
    string *type;
    string *value;
};


struct FOR_STATEMENT{
    // 这个变量是有关于检索到的变量名的，姑且命名为t01吧
    string *t01;
    string *condition;
};


class Analysis
{
public:

    static void Analysis_Command(const std::string &command) {
        VirtualMachineMain::init();
        SimpleTokenizer tokenizer(command);
        std::string token;
        std::string value;
        static FOR_STATEMENT* for_Statement = new FOR_STATEMENT();
        static SET_VAL* set_Val = new SET_VAL();
        string dp2;
        string v3_3;
        string prefix3;

        while (tokenizer.hasNextToken()) {
            token = tokenizer.getNextToken();
            if (token == "WHILE"){
                string expr;
                expr = tokenizer.getNextToken();
                //TODO!
            }
            // 加减乘除基本实现
            if (token == "FOR") {
                // 解析初始化
                if (tokenizer.hasNextToken()) {
                    // 获取变量名称
                    string name;
                    name = tokenizer.getNextToken();
                    set_Val->name = &name;
                    // 获取值
                    if (tokenizer.hasNextToken()) {
                        string type;
                        type = tokenizer.getNextToken();
                        set_Val->type = &type;
                        if (tokenizer.hasNextToken()) {
                            // 获取一个必要的前置符
                            string prefix;
                            prefix = tokenizer.getNextToken();
                            if (prefix == "VARIABLE") {
                                if (tokenizer.hasNextToken()) {
                                    string value;
                                    value = tokenizer.getNextToken();
                                    set_Val->value = &value;
                                    // 继续解析，解析条件
                                    if (tokenizer.hasNextToken()) {
                                        // 获取一下变量名
                                        string name2;
                                        name2 = tokenizer.getNextToken();
                                        for_Statement->t01 = &name2;
                                        // 获取是< >或者 =
                                        string prefix2;
                                        prefix2 = tokenizer.getNextToken();
                                        for_Statement->condition = &prefix2;
                                        // 继续获取，获取数据类型
                                        dp2 = tokenizer.getNextToken();
                                        // 继续获取，获取值
                                        v3_3 = tokenizer.getNextToken();

                                        // 获取一个基本运算符
                                        prefix3 = tokenizer.getNextToken();
                                    }

                                    if (set_Val->type[0] == "INT" && for_Statement->condition[0] == "LESS" &&
                                        dp2 == "INT" && prefix3 == "ADD") {
                                        string varname = set_Val->name->c_str();
                                        int v4 = stoi(set_Val->value[0]);
                                        int end_value = stoi(v3_3);
                                        for (int i = v4; i < end_value; i++) {
                                            VirtualMachineMain::vm->iax[i] = i;
                                            // 压入当前的变量值
                                        }
                                    } else if (set_Val->type[0] == "FLOAT" && for_Statement->condition[0] == "LESS" &&
                                               dp2 == "FLOAT" && prefix3 == "ADD") {
                                        string varname = set_Val->name->c_str();
                                        float v4 = stof(set_Val->value[0]);
                                        float end_value = stof(v3_3);
                                        for (float i = v4; i < end_value; i++) {
                                            VirtualMachineMain::vm->fax[i] = i;
                                            // 压入当前的变量值
                                        }
                                    } else if (set_Val->type[0] == "DOUBLE" && for_Statement->condition[0] == "LESS" &&
                                               dp2 == "DOUBLE" && prefix3 == "ADD") {
                                        string varname = set_Val->name->c_str();
                                        double v4 = stod(set_Val->value[0]);
                                        double end_value = stod(v3_3);
                                        for (double i = v4; i < end_value; i++) {
                                            VirtualMachineMain::vm->dax[i] = i;
                                            // 压入当前的变量值
                                        }
                                    } else if (set_Val->type[0] == "INT" && for_Statement->condition[0] == "LESS" &&
                                               dp2 == "INT" && prefix3 == "LESS") {
                                        string varname = set_Val->name->c_str();
                                        int v4 = stoi(set_Val->value[0]);
                                        int end_value = stoi(v3_3);
                                        for (int i = v4; i < end_value; i--) {
                                            VirtualMachineMain::vm->iax[i] = i;
                                            // 压入当前的变量值
                                        }
                                    } else if (set_Val->type[0] == "FLOAT" && for_Statement->condition[0] == "LESS" &&
                                               dp2 == "FLOAT" && prefix3 == "LESS") {
                                        string varname = set_Val->name->c_str();
                                        float v4 = stof(set_Val->value[0]);
                                        float end_value = stof(v3_3);
                                        for (float i = v4; i < end_value; i--) {
                                            VirtualMachineMain::vm->fax[i] = i;
                                            // 压入当前的变量值
                                        }
                                    } else if (set_Val->type[0] == "DOUBLE" && for_Statement->condition[0] == "LESS" &&
                                               dp2 == "DOUBLE" && prefix3 == "LESS") {
                                        string varname = set_Val->name->c_str();
                                        double v4 = stod(set_Val->value[0]);
                                        double end_value = stod(v3_3);
                                        for (double i = v4; i < end_value; i--) {
                                            VirtualMachineMain::vm->dax[i] = i;
                                            // 压入当前的变量值
                                        }
                                    } else if (set_Val->type[0] == "INT" && for_Statement->condition[0] == "GREATER" &&
                                               dp2 == "INT" && prefix3 == "ADD") {
                                        string varname = set_Val->name->c_str();
                                        int v4 = stoi(set_Val->value[0]);
                                        int end_value = stoi(v3_3);
                                        for (int i = v4; i > end_value; i++) {
                                            VirtualMachineMain::vm->iax[i] = i;
                                            // 压入当前的变量值
                                        }
                                    } else if (set_Val->type[0] == "FLOAT" &&
                                               for_Statement->condition[0] == "GREATER" && dp2 == "FLOAT" &&
                                               prefix3 == "ADD") {
                                        string varname = set_Val->name->c_str();
                                        float v4 = stof(set_Val->value[0]);
                                        float end_value = stof(v3_3);
                                        for (float i = v4; i > end_value; i++) {
                                            VirtualMachineMain::vm->fax[i] = i;
                                            // 压入当前的变量值
                                        }
                                    } else if (set_Val->type[0] == "DOUBLE" &&
                                               for_Statement->condition[0] == "GREATER" && dp2 == "DOUBLE" &&
                                               prefix3 == "ADD") {
                                        string varname = set_Val->name->c_str();
                                        double v4 = stod(set_Val->value[0]);
                                        double end_value = stod(v3_3);
                                        for (double i = v4; i > end_value; i++) {
                                            // 压入当前的变量值
                                        }
                                    } else if (set_Val->type[0] == "INT" && for_Statement->condition[0] == "GREATER" &&
                                               dp2 == "INT" && prefix3 == "LESS") {
                                        string varname = set_Val->name->c_str();
                                        int v4 = stoi(set_Val->value[0]);
                                        int end_value = stoi(v3_3);
                                        for (int i = v4; i > end_value; i--) {
                                            // 压入当前的变量值
                                        }
                                    } else if (set_Val->type[0] == "FLOAT" &&
                                               for_Statement->condition[0] == "GREATER" && dp2 == "FLOAT" &&
                                               prefix3 == "LESS") {
                                        string varname = set_Val->name->c_str();
                                        float v4 = stof(set_Val->value[0]);
                                        float end_value = stof(v3_3);
                                        for (float i = v4; i > end_value; i--) {
                                            VirtualMachineMain::vm->fax[i] = i;
                                            // 压入当前的变量值
                                        }
                                    } else if (set_Val->type[0] == "DOUBLE" &&
                                               for_Statement->condition[0] == "GREATER" && dp2 == "DOUBLE" &&
                                               prefix3 == "LESS") {
                                        string varname = set_Val->name->c_str();
                                        double v4 = stod(set_Val->value[0]);
                                        double end_value = stod(v3_3);
                                        for (double i = v4; i > end_value; i--) {
                                            VirtualMachineMain::vm->dax[i] = i;
                                            // 压入当前的变量值
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }


            // 实现VARIABLE CONSTANT <>(获取位空间) .FUNCTION
            if (token == "ADD") {
                if (tokenizer.hasNextToken()) {
                    std::string type = tokenizer.getNextToken();
                    if (tokenizer.hasNextToken()) {
                        std::string operand1_str = tokenizer.getNextToken();
                        if (tokenizer.hasNextToken()) {
                            std::string operand2_str = tokenizer.getNextToken();
                            if (type == "INT") {
                                int operand1 = stoi(operand1_str);
                                int operand2 = stoi(operand2_str);
                                int result = operand1 + operand2;
                                VirtualMachineMain::vm->iax[0] = result;
                            } else if (type == "DOUBLE") {
                                double operand1 = stod(operand1_str);
                                double operand2 = stod(operand2_str);
                                double result = operand1 + operand2;
                                VirtualMachineMain::vm->dax[0] = result;
                            } else if (type == "FLOAT") {
                                float operand1 = stof(operand1_str);
                                float operand2 = stof(operand2_str);
                                float result = operand1 + operand2;
                                VirtualMachineMain::vm->fax[0] = result;
                            }
                        }
                    }
                }
            }

            if (token == "SUB") {
                if (tokenizer.hasNextToken()) {
                    std::string type = tokenizer.getNextToken();
                    if (tokenizer.hasNextToken()) {
                        std::string operand1_str = tokenizer.getNextToken();
                        if (tokenizer.hasNextToken()) {
                            std::string operand2_str = tokenizer.getNextToken();
                            if (type == "INT") {
                                int operand1 = stoi(operand1_str);
                                int operand2 = stoi(operand2_str);
                                int result = operand1 - operand2;
                                VirtualMachineMain::vm->iax[0] = result;
                            } else if (type == "DOUBLE") {
                                double operand1 = stod(operand1_str);
                                double operand2 = stod(operand2_str);
                                double result = operand1 - operand2;
                                VirtualMachineMain::vm->dax[0] = result;
                            } else if (type == "FLOAT") {
                                float operand1 = stof(operand1_str);
                                float operand2 = stof(operand2_str);
                                float result = operand1 - operand2;
                                VirtualMachineMain::vm->fax[0] = result;
                            }
                        }
                    }
                }
            }

            if (token == "MUL") {
                if (tokenizer.hasNextToken()) {
                    std::string type = tokenizer.getNextToken();
                    if (tokenizer.hasNextToken()) {
                        std::string operand1_str = tokenizer.getNextToken();
                        if (tokenizer.hasNextToken()) {
                            std::string operand2_str = tokenizer.getNextToken();
                            if (type == "INT") {
                                int operand1 = stoi(operand1_str);
                                int operand2 = stoi(operand2_str);
                                int result = operand1 * operand2;
                                VirtualMachineMain::vm->iax[0] = result;
                            } else if (type == "DOUBLE") {
                                double operand1 = stod(operand1_str);
                                double operand2 = stod(operand2_str);
                                double result = operand1 * operand2;
                                VirtualMachineMain::vm->dax[0] = result;
                            } else if (type == "FLOAT") {
                                float operand1 = stof(operand1_str);
                                float operand2 = stof(operand2_str);
                                float result = operand1 * operand2;
                                VirtualMachineMain::vm->fax[0] = result;
                            }
                        }
                    }
                }
            }

            if (token == "DIV") {
                if (tokenizer.hasNextToken()) {
                    std::string type = tokenizer.getNextToken();
                    if (tokenizer.hasNextToken()) {
                        std::string operand1_str = tokenizer.getNextToken();
                        if (tokenizer.hasNextToken()) {
                            std::string operand2_str = tokenizer.getNextToken();
                            if (type == "INT") {
                                int operand1 = stoi(operand1_str);
                                int operand2 = stoi(operand2_str);
                                int result = operand1 / operand2;
                                VirtualMachineMain::vm->iax[0] = result;
                            } else if (type == "DOUBLE") {
                                double operand1 = stod(operand1_str);
                                double operand2 = stod(operand2_str);
                                double result = operand1 / operand2;
                                VirtualMachineMain::vm->dax[0] = result;
                            } else if (type == "FLOAT") {
                                float operand1 = stof(operand1_str);
                                float operand2 = stof(operand2_str);
                                float result = operand1 / operand2;
                                VirtualMachineMain::vm->fax[0] = result;
                            }
                        }
                    }
                }
            }

            // 实现PERCENT
            if (token == "PERCENT") {
                if (tokenizer.hasNextToken()) {
                    std::string type = tokenizer.getNextToken();
                    if (tokenizer.hasNextToken()) {
                        std::string operand1_str = tokenizer.getNextToken();
                        if (tokenizer.hasNextToken()) {
                            std::string operand2_str = tokenizer.getNextToken();
                            if (type == "INT") {
                                int operand1 = stoi(operand1_str);
                                int operand2 = stoi(operand2_str);
                                int result = operand1 % operand2;
                                VirtualMachineMain::vm->iax[0] = result;
                            } else if (type == "DOUBLE") {
                                double operand1 = stod(operand1_str);
                                double operand2 = stod(operand2_str);
                                double result = fmod(operand1, operand2); // 使用 fmod 计算浮点数的余数
                                VirtualMachineMain::vm->dax[0] = result;
                            } else if (type == "FLOAT") {
                                float operand1 = stof(operand1_str);
                                float operand2 = stof(operand2_str);
                                float result = fmod(operand1, operand2); // 使用 fmod 计算浮点数的余数
                                VirtualMachineMain::vm->fax[0] = result;
                            }
                        }
                    }
                }
            }

            if (token == "AND") {
                if (tokenizer.hasNextToken()) {
                    std::string type = tokenizer.getNextToken();
                    if (tokenizer.hasNextToken()) {
                        std::string operand1_str = tokenizer.getNextToken();
                        if (tokenizer.hasNextToken()) {
                            std::string operand2_str = tokenizer.getNextToken();
                            if (type == "INT") {
                                int operand1 = stoi(operand1_str);
                                int operand2 = stoi(operand2_str);
                                int result = operand1 && operand2;
                                VirtualMachineMain::setRegister(0, result);
                            } else if (type == "DOUBLE") {
                                double operand1 = stod(operand1_str);
                                double operand2 = stod(operand2_str);
                                double result = operand1 && operand2;
                                VirtualMachineMain::setRegister(0, result);
                            } else if (type == "FLOAT") {
                                float operand1 = stof(operand1_str);
                                float operand2 = stof(operand2_str);
                                float result = operand1 && operand2;
                                VirtualMachineMain::setRegister(0, result);
                            } else if (type == "CHAR") {
                                char operand1 = operand1_str[0];
                                char operand2 = operand2_str[0];
                                char result = operand1 && operand2;
                                VirtualMachineMain::setRegister(0, result);
                            } else if (type == "BOOL") {
                                bool operand1 = (operand1_str == "TRUE");
                                bool operand2 = (operand2_str == "TRUE");
                                bool result = operand1 && operand2;
                                VirtualMachineMain::setRegister(0, result);
                            } else if (type == "STRING") {
                                bool operand1 = !operand1_str.empty();
                                bool operand2 = !operand2_str.empty();
                                bool result = operand1 && operand2;
                                VirtualMachineMain::setRegister(0, result);
                            }
                        }
                    }
                }
            }

            // 实现GREATER LESS EQUAL NOT_EQUAL NOT
            if (token == "LESS"){
                if (tokenizer.hasNextToken()){
                    std::string type = tokenizer.getNextToken();
                    if (tokenizer.hasNextToken()) {
                        std::string operand1_str = tokenizer.getNextToken();
                        if (tokenizer.hasNextToken()) {
                            std::string operand2_str = tokenizer.getNextToken();
                            if (type == "INT") {
                                int operand1 = stoi(operand1_str);
                                int operand2 = stoi(operand2_str);
                                VirtualMachineMain::execute("LESS", operand1, operand2, 0);
                                VirtualMachineMain::ExecuteVirtualMachine_BC("PUSH", "IAX", &operand1);
                                VirtualMachineMain::ExecuteVirtualMachine_BC("PUSH", "IAX", &operand2);
                            } else if (type == "DOUBLE") {
                                double operand1 = stod(operand1_str);
                                double operand2 = stod(operand2_str);
                                VirtualMachineMain::execute("LESS", operand1, operand2, 0);
                                VirtualMachineMain::ExecuteVirtualMachine_BC("PUSH", "DAX", &operand1);
                                VirtualMachineMain::ExecuteVirtualMachine_BC("PUSH", "DAX", &operand2);
                            } else if (type == "FLOAT") {
                                float operand1 = stof(operand1_str);
                                float operand2 = stof(operand2_str);
                                VirtualMachineMain::execute("LESS", operand1, operand2, 0);
                                VirtualMachineMain::ExecuteVirtualMachine_BC("PUSH", "FAX", &operand1);
                                VirtualMachineMain::ExecuteVirtualMachine_BC("PUSH", "FAX", &operand2);
                            }
                        }
                    }
                }
            }

            if (token == "GREATER"){
                if (tokenizer.hasNextToken()){
                    std::string type = tokenizer.getNextToken();
                    if (tokenizer.hasNextToken()) {
                        std::string operand1_str = tokenizer.getNextToken();
                        if (tokenizer.hasNextToken()) {
                            std::string operand2_str = tokenizer.getNextToken();
                            if (type == "INT") {
                                int operand1 = stoi(operand1_str);
                                int operand2 = stoi(operand2_str);
                                VirtualMachineMain::execute("GREATER", operand1, operand2, 0);
                                VirtualMachineMain::ExecuteVirtualMachine_BC("PUSH", "IAX", &operand1);
                                VirtualMachineMain::ExecuteVirtualMachine_BC("PUSH", "IAX", &operand2);
                            } else if (type == "DOUBLE") {
                                double operand1 = stod(operand1_str);
                                double operand2 = stod(operand2_str);
                                VirtualMachineMain::execute("GREATER", operand1, operand2, 0);
                                VirtualMachineMain::ExecuteVirtualMachine_BC("PUSH", "DAX", &operand1);
                                VirtualMachineMain::ExecuteVirtualMachine_BC("PUSH", "DAX", &operand2);
                            } else if (type == "FLOAT") {
                                float operand1 = stof(operand1_str);
                                float operand2 = stof(operand2_str);
                                VirtualMachineMain::execute("GREATER", operand1, operand2, 0);
                                VirtualMachineMain::ExecuteVirtualMachine_BC("PUSH", "FAX", &operand1);
                                VirtualMachineMain::ExecuteVirtualMachine_BC("PUSH", "FAX", &operand2);
                            }
                        }
                    }
                }
            }

            if (token == "EQUAL") {
                if (tokenizer.hasNextToken()) {
                    std::string type = tokenizer.getNextToken();
                    if (tokenizer.hasNextToken()) {
                        std::string operand1_str = tokenizer.getNextToken();
                        if (tokenizer.hasNextToken()) {
                            std::string operand2_str = tokenizer.getNextToken();
                            if (type == "INT") {
                                int operand1 = stoi(operand1_str);
                                int operand2 = stoi(operand2_str);
                                int result = (operand1 == operand2);
                                VirtualMachineMain::setRegister(0, result);
                            } else if (type == "DOUBLE") {
                                double operand1 = stod(operand1_str);
                                double operand2 = stod(operand2_str);
                                double result = (operand1 == operand2);
                                VirtualMachineMain::setRegister(0, result);
                            } else if (type == "FLOAT") {
                                float operand1 = stof(operand1_str);
                                float operand2 = stof(operand2_str);
                                float result = (operand1 == operand2);
                                VirtualMachineMain::setRegister(0, result);
                            } else if (type == "CHAR") {
                                char operand1 = operand1_str[0];
                                char operand2 = operand2_str[0];
                                char result = (operand1 == operand2);
                                VirtualMachineMain::setRegister(0, result);
                            } else if (type == "BOOL") {
                                bool operand1 = (operand1_str == "TRUE");
                                bool operand2 = (operand2_str == "TRUE");
                                bool result = (operand1 == operand2);
                                VirtualMachineMain::setRegister(0, result);
                            } else if (type == "STRING") {
                                bool result = (operand1_str == operand2_str);
                                VirtualMachineMain::setRegister(0, result);
                            }
                        }
                    }
                }
            }


            /**
             * NOT EQUAL
             */
             if (token == "NOTEQUAL") {
                if (tokenizer.hasNextToken()) {
                    std::string type = tokenizer.getNextToken();
                    if (tokenizer.hasNextToken()) {
                        std::string operand1_str = tokenizer.getNextToken();
                        if (tokenizer.hasNextToken()) {
                            std::string operand2_str = tokenizer.getNextToken();
                            if (type == "INT") {
                                int operand1 = stoi(operand1_str);
                                int operand2 = stoi(operand2_str);
                                int result = (operand1 != operand2);
                                VirtualMachineMain::setRegister(0, result);
                            } else if (type == "DOUBLE") {
                                double operand1 = stod(operand1_str);
                                double operand2 = stod(operand2_str);
                                double result = (operand1 != operand2);
                                VirtualMachineMain::setRegister(0, result);
                            } else if (type == "FLOAT") {
                                float operand1 = stof(operand1_str);
                                float operand2 = stof(operand2_str);
                                float result = (operand1 != operand2);
                                VirtualMachineMain::setRegister(0, result);
                            } else if (type == "CHAR") {
                                char operand1 = operand1_str[0];
                                char operand2 = operand2_str[0];
                                char result = (operand1 != operand2);
                                VirtualMachineMain::setRegister(0, result);
                            } else if (type == "BOOL") {
                                bool operand1 = (operand1_str == "TRUE");
                                bool operand2 = (operand2_str == "TRUE");
                                bool result = (operand1 != operand2);
                                VirtualMachineMain::setRegister(0, result);
                            } else if (type == "STRING") {
                                bool result = (operand1_str != operand2_str);
                                VirtualMachineMain::setRegister(0, result);
                            }
                        }
                    }
                }
            }


            if (token == "NOT") {
                if (tokenizer.hasNextToken()) {
                    std::string type = tokenizer.getNextToken();
                    if (tokenizer.hasNextToken()) {
                        std::string operand1_str = tokenizer.getNextToken();
                        if (type == "INT") {
                            int operand1 = stoi(operand1_str);
                            int result = !operand1;
                            VirtualMachineMain::setRegister(0, result);
                        } else if (type == "DOUBLE") {
                            double operand1 = stod(operand1_str);
                            double result = !operand1;
                            VirtualMachineMain::setRegister(0, result);
                        } else if (type == "FLOAT") {
                            float operand1 = stof(operand1_str);
                            float result = !operand1;
                            VirtualMachineMain::setRegister(0, result);
                        } else if (type == "CHAR") {
                            char operand1 = operand1_str[0];
                            char result = !operand1;
                            VirtualMachineMain::setRegister(0, result);
                        } else if (type == "BOOL") {
                            bool operand1 = (operand1_str == "TRUE");
                            bool result = !operand1;
                            VirtualMachineMain::setRegister(0, result);
                        } else if (type == "STRING") {
                            bool operand1 = !operand1_str.empty();
                            VirtualMachineMain::setRegister(0, operand1);
                        }
                    }
                }
            }


            if (token == "NOT") {
                if (tokenizer.hasNextToken()) {
                    std::string type = tokenizer.getNextToken();
                    if (tokenizer.hasNextToken()) {
                        std::string operand1_str = tokenizer.getNextToken();
                        if (type == "INT") {
                            int operand1 = stoi(operand1_str);
                            int result = !operand1;
                            VirtualMachineMain::setRegister(0, result);
                        } else if (type == "DOUBLE") {
                            double operand1 = stod(operand1_str);
                            double result = !operand1;
                            VirtualMachineMain::setRegister(0, result);
                        } else if (type == "FLOAT") {
                            float operand1 = stof(operand1_str);
                            float result = !operand1;
                            VirtualMachineMain::setRegister(0, result);
                        } else if (type == "CHAR") {
                            char operand1 = operand1_str[0];
                            char result = !operand1;
                            VirtualMachineMain::setRegister(0, result);
                        } else if (type == "BOOL") {
                            bool operand1 = (operand1_str == "TRUE");
                            bool result = !operand1;
                            VirtualMachineMain::setRegister(0, result);
                        } else if (type == "STRING") {
                            bool operand1 = !operand1_str.empty();
                            VirtualMachineMain::setRegister(0, operand1);
                        }
                    }
                }
            }

            if (token == "OR") {
                if (tokenizer.hasNextToken()) {
                    std::string type = tokenizer.getNextToken();
                    if (tokenizer.hasNextToken()) {
                        std::string operand1_str = tokenizer.getNextToken();
                        if (tokenizer.hasNextToken()) {
                            std::string operand2_str = tokenizer.getNextToken();
                            if (type == "INT") {
                                int operand1 = stoi(operand1_str);
                                int operand2 = stoi(operand2_str);
                                int result = operand1 || operand2;
                                VirtualMachineMain::setRegister(0, result);
                            } else if (type == "DOUBLE") {
                                double operand1 = stod(operand1_str);
                                double operand2 = stod(operand2_str);
                                double result = operand1 || operand2;
                                VirtualMachineMain::setRegister(0, result);
                            } else if (type == "FLOAT") {
                                float operand1 = stof(operand1_str);
                                float operand2 = stof(operand2_str);
                                float result = operand1 || operand2;
                                VirtualMachineMain::setRegister(0, result);
                            } else if (type == "CHAR") {
                                char operand1 = operand1_str[0];
                                char operand2 = operand2_str[0];
                                char result = operand1 || operand2;
                                VirtualMachineMain::setRegister(0, result);
                            } else if (type == "BOOL") {
                                bool operand1 = (operand1_str == "TRUE");
                                bool operand2 = (operand2_str == "TRUE");
                                bool result = operand1 || operand2;
                                VirtualMachineMain::setRegister(0, result);
                            } else if (type == "STRING") {
                                bool operand1 = !operand1_str.empty();
                                bool operand2 = !operand2_str.empty();
                                bool result = operand1 || operand2;
                                VirtualMachineMain::setRegister(0, result);
                            }
                        }
                    }
                }
            }

            if (token == "CMP"){
                // COMPARE实现
                if (tokenizer.hasNextToken()){
                    string Type2;
                    Type2 = tokenizer.getNextToken();
                    string valuable;
                    valuable = tokenizer.getNextToken();
                    if (Type2 == "INT"){
                        int result = stoi(valuable);
                        VirtualMachineMain::ExecuteVirtualMachine_BC("CMP" , "", &result);
                    } else if (Type2 == "DOUBLE"){
                        double result = stod(valuable);
                        VirtualMachineMain::ExecuteVirtualMachine_BC("CMP" , "", &result);
                    } else if (Type2 == "FLOAT"){
                        float result = stof(valuable);
                        VirtualMachineMain::ExecuteVirtualMachine_BC("CMP" , "", &result);
                    } else if (Type2 == "CHAR"){
                        char result = valuable[0];
                        VirtualMachineMain::ExecuteVirtualMachine_BC("CMP" , "", &result);
                    } else if (Type2 == "BOOL"){
                        if (valuable == "true"){
                            bool result = true;
                            VirtualMachineMain::ExecuteVirtualMachine_BC("CMP" , "", &result);
                        } else {
                            bool result = false;
                            VirtualMachineMain::ExecuteVirtualMachine_BC("CMP" , "", &result);
                        }
                    } else if (Type2 == "STRING"){
                        VirtualMachineMain::ExecuteVirtualMachine_BC("CMP" , "", &valuable);
                    } else if (Type2 == "EMPTY"){
                        VirtualMachineMain::ExecuteVirtualMachine_BC("CMP" , "", nullptr);
                    }
                }
            }
            /**
             * POP IMPL
             * ??? 不是PUSH进去了吗
             */
            if (token == "POP"){
                if (tokenizer.hasNextToken()){
                    string mode = tokenizer.getNextToken();
                    if (mode == "VIEW"){
                        string get_reg_con = tokenizer.getNextToken();
                        VirtualMachineMain::PrintStack(get_reg_con);
                    } else {
                        string get_reg_con = tokenizer.getNextToken();
                        VirtualMachineMain::ExecuteVirtualMachine_BC("POP", get_reg_con, nullptr);
                    }
                }
            }
            // 实现POP MOV PUSH
            if (token == "MOV"){
                if (tokenizer.hasNextToken()){
                    // 获取寄存器
                    token = tokenizer.getNextToken();
                    value = tokenizer.getNextToken();
                }
            }
            // 判断寄存器类型
            if (token == "IAX"){
                int result = stoi(value);
                VirtualMachineMain::ExecuteVirtualMachine_BC("MOV", token, &result);
            } else if (token == "DAX"){
                double result = stod(value);
                VirtualMachineMain::ExecuteVirtualMachine_BC("MOV", token, &result);
            } else if (token == "SAX"){
                VirtualMachineMain::ExecuteVirtualMachine_BC("MOV", token, &value);
            } else if (token == "FAX"){
                float result = stof(value);
                VirtualMachineMain::ExecuteVirtualMachine_BC("MOV", token, &result);
            } else if (token == "CAX"){
                char result = value[0];
                VirtualMachineMain::ExecuteVirtualMachine_BC("MOV", token, &result);
            } else if (token == "BAX"){
                if (value == "true"){
                    bool result = true;
                    VirtualMachineMain::ExecuteVirtualMachine_BC("MOV", token, &result);
                } else {
                    bool result = false;
                    VirtualMachineMain::ExecuteVirtualMachine_BC("MOV", token, &result);
                }
            } else if (token == "AX"){
                VirtualMachineMain::ExecuteVirtualMachine_BC("MOV", token, &value);
            } else if (token == "PUSH"){
                if (tokenizer.hasNextToken()) {
                    string type;
                    string push_val;
                    type = tokenizer.getNextToken();
                    push_val = tokenizer.getNextToken();
                    if (type == "INT"){
                        int result = stoi(push_val);
                        VirtualMachineMain::ExecuteVirtualMachine_BC("PUSH", "IAX", &result);
                    } else if (type == "DOUBLE"){
                        double result = stod(push_val);
                        VirtualMachineMain::ExecuteVirtualMachine_BC("PUSH", "DAX", &result);
                    } else if (type == "FLOAT"){
                        float result = stof(push_val);
                        VirtualMachineMain::ExecuteVirtualMachine_BC("PUSH", "FAX", &result);
                    } else if (type == "CHAR"){
                        char result = push_val[0];
                        VirtualMachineMain::ExecuteVirtualMachine_BC("PUSH", "CAX", &result);
                    } else if (type == "BOOL"){
                        if (push_val == "true"){
                            bool result = true;
                            VirtualMachineMain::ExecuteVirtualMachine_BC("PUSH", "BAX", &result);
                        } else {
                            bool result = false;
                            VirtualMachineMain::ExecuteVirtualMachine_BC("PUSH", "BAX", &result);
                        }
                    } else if (type == "STRING"){
                        VirtualMachineMain::ExecuteVirtualMachine_BC("PUSH", "SAX", &push_val);
                    } else if (type == "EMPTY"){
                        VirtualMachineMain::ExecuteVirtualMachine_BC("PUSH", "AX", nullptr);
                    }
                }
            }
        }
    }
};
